# $Id: PKGBUILD 89232 2013-04-28 06:44:20Z tpowa $
# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Based on SUSE spec https://build.opensuse.org/package/files?package=rt3562sta&project=driver%3Awireless

pkgname=rt3562sta
pkgver=2.4.1.1
pkgrel=28
pkgdesc="Ralink RT3562 PCI WLAN adaptors kernel module"
arch=(i686 x86_64)
url="http://www.mediatek.com/en/Products/support.php?sn=501"
license=('GPL')
depends=('linux')
makedepends=('linux-headers')
install=$pkgname.install
source=(
	http://dl.dropbox.com/u/362439/DPO_RT3562_3592_3062_LinuxSTA_V${pkgver}_20101217.tgz
	$pkgname-$pkgver-config.patch
	$pkgname-$pkgver-gcc-warnings-x86_64.patch
	$pkgname-$pkgver-WPA-mixed.patch
	$pkgname-$pkgver-convert-devicename-to-wlanX.patch
	$pkgname-$pkgver-remove-potential-conflicts-with-rt2860sta.patch
	$pkgname-$pkgver-return_nonvoid.patch
	$pkgname-$pkgver-reduce_debug_output.patch
	$pkgname-$pkgver-remove_date_time.patch
	linux-3.8.patch
)

build() {
	_kernver=$(pacman -Q linux | cut -d . -f 2 | cut -f 1 -d -)
	KERNEL_RELEASE=$(cat /usr/lib/modules/extramodules-3.$_kernver-ARCH/version)

	cd "$srcdir/DPO_RT3562_3592_3062_LinuxSTA_V2.4.1.1_20101217"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-config.patch"
	[ "$CARCH" == "x86_64" ] && patch -p0 -i "$srcdir/$pkgname-$pkgver-gcc-warnings-x86_64.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-WPA-mixed.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-convert-devicename-to-wlanX.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-remove-potential-conflicts-with-rt2860sta.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-return_nonvoid.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-reduce_debug_output.patch"
	patch -p0 -i "$srcdir/$pkgname-$pkgver-remove_date_time.patch"
	patch -p1 -i "$srcdir/linux-3.8.patch"

	# clean up this mess of mixing RT2860STA with RT3562STA
	# in documentation files
	mv RT2860STA.dat RT3562STA.dat
	mv RT2860STACard.dat RT3562STACard.dat
	sed -i 's/2860/3562/g' *STA* iwpriv_usage.txt

	# as we change the default name of the interface from raX to wlanX, change respective references in documentation, too
	sed -i 's|ra0|wlan0|g' *.txt README* *.dat
	sed -i 's|ra1|wlan1|g' *.txt README* *.dat
	sed -i 's|ra2|wlan2|g' *.txt README* *.dat

	export EXTRA_CFLAGS="-DVERSION=$pkgver"

	# this Makefile is far too strict...
	echo "LINUX_SRC = /usr/lib/modules/$KERNEL_RELEASE/build" >> Makefile

	make
}

package() {
	_kernver=$(pacman -Q linux | cut -d . -f 2 | cut -f 1 -d -)
	depends=("linux>=3.${_kernver}" "linux<3.$(expr ${_kernver} + 1)")
	KERNEL_VERSION=$(cat /usr/lib/modules/extramodules-3.$_kernver-ARCH/version)
	msg "Kernel = $KERNEL_VERSION"

	cd "$srcdir/DPO_RT3562_3592_3062_LinuxSTA_V2.4.1.1_20101217"

	install -Dm 0640 RT3562STA.dat "$pkgdir/etc/Wireless/RT3562STA/RT3562STA.dat"
	install -Dm 0644 os/linux/$pkgname.ko "$pkgdir/usr/lib/modules/extramodules-3.$_kernver-ARCH/$pkgname.ko"
	install -dm 0755 "$pkgdir/usr/share/doc/$pkgname"
	install -m 0644 iwpriv_usage.txt README* RT3562STA* sta_ate_iwpriv_usage.txt "$pkgdir/usr/share/doc/$pkgname"

	find "$pkgdir" -name '*.ko' -exec gzip -9 {} \;
	sed -i "s|extramodules-.*-ARCH|extramodules-3.$_kernver-ARCH|" "$startdir/$pkgname.install"
}

sha256sums=('71cd27784009894198f03470ec88e77f2cd176bf5f8d93b4dd984d3a3e543d5a'
            'b1465fceaac99f26cc0e7db0562d2bf103b57656f0b7b57e404f59efb3373b27'
            'd013ed5981a02be27b4aaf1e03670d68dbb16f40d9eb84930e58a96fc38ef947'
            'c962d014ea6e84a8c3398ff13e4709cc309c29134cac0b5c702eab1026799887'
            'e966d96026baee0447d6ad26742bcc40baa3f94530573d58f4eca842e107f809'
            '7d2a83edfe6664a8e923dcda636c871220efc7ff4a82ab76a62fc97a11f2fcaa'
            'd672b58cdb278e3ecaeb46f3e5e477e1da7d2fd5dbf896521a4d0993743b9ab9'
            '4d1d5eb5c22d356af62f4f55d12b04eb8a557ea9c664edfbc79c9e0454ccca4e'
            'a888d045b142127070c2c23ed1a84306d5202896265c828cf7e125ec2dd52389'
            'ce44f33ab9e0cb407718e3bef14b70be9c5fe6636cf8a483c1d8c891c6bfdbd8')
